// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/SingleObjectiveHintWidget.h"
#include "Components/Border.h"
#include "Components/TextBlock.h"
#include "Components/VerticalBox.h"
#include "Materials/MaterialInstance.h"

void USingleObjectiveHintWidget::NativeOnInitialized() {
	Super::NativeOnInitialized();
	TextColor = FLinearColor(0.443137f, 0.360784f, 0.098039f, 1.f);
}

void USingleObjectiveHintWidget::NativeConstruct() {
	Super::NativeConstruct();
	ObjectiveTitle->SetColorAndOpacity(FSlateColor(TextColor));
	ObjectiveTitle->SetText(ObjectiveText);
	ObjectiveIcon->SetBrushFromMaterial(Hints.HintMarker);
	ListBox->ClearChildren();
	for (int32 i = 0; i < Hints.Hints.Num();++i) {
		USingleHintWidget* Temp = Cast<USingleHintWidget>(CreateWidget(this, HintWidget));
		if (Temp) {
			Temp->ConstructWidget(Hints.Hints[i]);
			ListBox->AddChild(Temp);
		}
	}
}

void USingleObjectiveHintWidget::ConstructWidget(FText LObjectiveText, FHintBase LHints, TSubclassOf<USingleHintWidget> LHintWidget) {
	ObjectiveText = LObjectiveText;
	Hints = LHints;
	HintWidget = LHintWidget;
}

void USingleObjectiveHintWidget::UpdateTextColor(bool Completed) {
	if (GetWorld()->GetTimerManager().IsTimerActive(TimerHandle)) {
		GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
	}
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &USingleObjectiveHintWidget::DelayLater, Completed);
	GetWorld()->GetTimerManager().SetTimer(TimerHandle, RespawnDelegate, 0.001f, false);
}

void USingleObjectiveHintWidget::DelayLater(bool Completed) {
	ObjectiveTitle->SetColorAndOpacity(FSlateColor(Completed ? TextColor : FLinearColor(1.f, 1.f, 1.f, 1.f)));
	GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
}

